package com.mrdeer.user.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mrdeer.common.domain.BusinessException;
import com.mrdeer.common.domain.PageVo;
import com.mrdeer.user.api.MenuService;
import com.mrdeer.user.api.dto.menu.MenuDTO;
import com.mrdeer.user.api.dto.menu.MenuQueryDTO;
import com.mrdeer.user.api.entity.ResourceMenu;
import com.mrdeer.user.convert.ResourceMenuConvert;
import com.mrdeer.user.mapper.ResourceMenuMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.List;

/**
 * @author ：xie jianchu
 * @date ：Created in 2020/9/7 0007 17:20
 */
@Slf4j
@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    private ResourceMenuMapper resourceMenuMapper;

    /**
     * 根据ID查询菜单
     *
     * @param id
     * @return
     * @throws BusinessException
     */
    @Override
    public MenuDTO queryMenu(Long id) throws BusinessException {
        ResourceMenu resourceMenu = resourceMenuMapper.selectById(id);
        MenuDTO menuDTO = ResourceMenuConvert.INSTANCE.entity2Dto(resourceMenu);
        return menuDTO;
    }

    /**
     * 根据应用编码查询菜单列表
     *
     * @param applicationCode
     * @return
     * @throws BusinessException
     */
    @Override
    public List<MenuDTO> queryMenuByApplicationCode(String applicationCode) throws BusinessException {
        List<ResourceMenu> resourceMenus = resourceMenuMapper.selectList(new QueryWrapper<ResourceMenu>().lambda()
                .eq(ResourceMenu::getApplicationCode, applicationCode));
        List<MenuDTO> menuDTOS = ResourceMenuConvert.INSTANCE.entityList2Dto(resourceMenus);
        return menuDTOS;
    }

    /**
     * 根据条件查询菜单列表
     *
     * @param params
     * @param pageNo
     * @param pageSize
     * @return
     * @throws BusinessException
     */
    @Override
    public PageVo<MenuDTO> queryMenu(MenuQueryDTO params, Integer pageNo, Integer pageSize) throws BusinessException {
        //参数applicationCode(app表) title status(菜单表)
        pageSize = (pageSize == null ? 20 : pageSize);
        Page<MenuDTO> page = new Page<>(pageNo, pageSize);     //当前页，总条数 构造 page 对象
        List<MenuDTO> menus = resourceMenuMapper.selectByPage(page, params);
        return new PageVo<>(menus, page.getTotal(), pageNo, pageSize);
    }

    /**
     * 根据权限编码列表获取菜单
     *
     * @param privileges 权限编码
     * @return
     * @throws BusinessException
     */
    @Override
    public List<MenuDTO> queryMenuByPrivileges(String[] privileges) throws BusinessException {
        List<String> privilege = Arrays.asList(privileges);
        List<ResourceMenu> resourceMenus = resourceMenuMapper.selectList(new QueryWrapper<ResourceMenu>().lambda()
                .in(ResourceMenu::getPrivilegeCode, privilege));
        List<MenuDTO> menuDTOS = ResourceMenuConvert.INSTANCE.entityList2Dto(resourceMenus);
        return menuDTOS;
    }
}
